package com.atguigu.tingshu.order.context;

import com.atguigu.tingshu.order.interceptor.OrderHandlerInterceptor;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class   OrderHandlerInterceptorContext implements ApplicationContextAware {

    // 创建一个List集合存储任务接口的实现类对象
    private static final List<OrderHandlerInterceptor> ORDER_HANDLER_INTERCEPTORS = new ArrayList<>() ;

    public static final OrderInfoVo entry(OrderInfoVo orderInfoVo){
        for(OrderHandlerInterceptor orderHandlerInterceptor : ORDER_HANDLER_INTERCEPTORS) {
            orderInfoVo = orderHandlerInterceptor.sbumitOrderRequestProcess(orderInfoVo) ;
        }
        return orderInfoVo ;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, OrderHandlerInterceptor> beansOfType = applicationContext.getBeansOfType(OrderHandlerInterceptor.class);
        beansOfType.values().stream().sorted((o1 , o2) -> o1.sort() - o2.sort()).forEach(orderHandlerInterceptor -> {
            ORDER_HANDLER_INTERCEPTORS.add(orderHandlerInterceptor) ;
        });
        log.info("把任务接口的实现类对象，都添加了list集合中...");
    }
}
